package com.deer.wms.instock.web;

import cn.hutool.core.util.ObjectUtil;
import com.deer.wms.ASN.manage.dao.AsnDetailMapper;
import com.deer.wms.ASN.manage.dao.AsnMasterMapper;
import com.deer.wms.ASN.manage.model.asn.AsnMaster;
import com.deer.wms.ASN.manage.service.AcceptRecordService;
import com.deer.wms.base.system.dao.SuppInfoMapper;
import com.deer.wms.base.system.service.CellInfoService;
import com.deer.wms.base.system.service.ItemInfoService;
import com.deer.wms.busine.tactic.service.UpperTacticService;
import com.deer.wms.instock.dao.TransferBillMapper;
import com.deer.wms.instock.model.Allot.AllotMaster;
import com.deer.wms.instock.model.disptch.DispatCriteria;
import com.deer.wms.instock.service.TransferBillService;
import com.deer.wms.instock.service.enter.StrategyEnterService;
import com.deer.wms.intercept.annotation.User;
import com.deer.wms.intercept.common.data.CurrentUser;
import com.deer.wms.inventory.model.Inventory.InventoryDto;
import com.deer.wms.inventory.service.InventoryService;
import com.deer.wms.project.root.core.result.Result;
import com.deer.wms.project.root.core.result.ResultGenerator;
import com.deer.wms.project.root.core.service.QueryCriteria;
import com.deer.wms.system.manage.service.DeptService;
import com.deer.wms.ware.task.model.SO.SoDetail;
import com.deer.wms.ware.task.model.SO.SoMaster;
import com.deer.wms.ware.task.model.SO.contrast.ContrastVo;
import com.deer.wms.ware.task.model.SO.vo.SoDetailVo;
import com.deer.wms.ware.task.model.SO.vo.SoMasterVo;
import com.deer.wms.ware.task.service.PickTaskService;
import com.deer.wms.ware.task.service.SoMasterService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: xu
 * @Date: 2022/06/21/15:46
 * @Description: 齐套性检查
 */
@Api(description = "齐套性检查")
@RestController
@RequestMapping("/dispatch")
public class DispatchController {

    @Autowired
    private TransferBillService transferBillService;

    @ApiOperation(value = "分页列表")
    @GetMapping("/list")
    public Result list(DispatCriteria criteria, @ApiIgnore @User CurrentUser currentUser) {
        PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize());

        List<String> billNos = new ArrayList<>();
        if (currentUser.getIsAdmin() != 1) {
        criteria.setDeptId(currentUser.getDeptId());
        }

        if (criteria.getOrderBillNo() != null && !criteria.getOrderBillNo().equals("")) {
            String billnoArr = criteria.getOrderBillNo();
            // 按照逗号分隔成多个任务单号
            String[] bills = billnoArr.split("\\s+");
            for (String billNo : bills) {
                billNos.add(billNo.trim());
            }
            criteria.setBillNos(billNos);
        }
        List<AsnMaster> list = transferBillService.alignList(criteria);
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }


    @ApiModelProperty("根据任务单号查询")
    @GetMapping("/detail")
    public Result detail(DispatCriteria criteria) {
        List<SoDetail> soDetail = transferBillService.detail(criteria.getOrderBillNos());
        if (ObjectUtil.isEmpty(soDetail)) {
             return ResultGenerator.genSuccessResult(new PageInfo<>());
        }
        List<SoDetail> soDetails = soDetail.stream().filter(
                item->
                     item.getItemCode().contains(criteria.getKeyWords())
        ).sorted(Comparator.comparing(SoDetail::getItemCode,Comparator.nullsLast(String::compareTo))).skip((criteria.getPageNum() - 1) * criteria.getPageSize()).limit(criteria.getPageSize()).collect(Collectors.toList());
        PageInfo pageInfo = new PageInfo(soDetails);
        pageInfo.setTotal(soDetail.size());
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @ApiModelProperty("根据任务单号查询")
    @GetMapping("/beBeing")
    public Result beBeing(ContrastVo contrastVo, @ApiIgnore @User CurrentUser currentUser) {

        List<Integer> list = new ArrayList<>();
        list.add(349805);
        list.add(107);
        if (currentUser.getIsAdmin() != 1) {
            Integer deptId = currentUser.getDeptId();
            if (list.contains(deptId)) {
                contrastVo.setDeptIds(list);
            } else {
                ArrayList<Integer> newList = new ArrayList<>();
                newList.add(currentUser.getDeptId());
                contrastVo.setDeptIds(newList);

            }
            ;


        }
      List<SoDetail> soDetails =  transferBillService.beBeing(contrastVo);

      return ResultGenerator.genSuccessResult(soDetails);
    }

    @PostMapping("/dispatch")
    @ApiModelProperty("齐套提交")
    private Result dispatch(@RequestBody SoDetailVo soDetailVo) {
        transferBillService.dispatch(soDetailVo);
        return ResultGenerator.genSuccessResult();
    }
}
